# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=evil-winrm
pkgver=v3.7.r0.gffe958c
pkgrel=4
epoch=1
pkgdesc='The ultimate WinRM shell for hacking/pentesting.'
arch=('any')
groups=('blackarch' 'blackarch-exploitation' 'blackarch-backdoor')
url='https://github.com/Hackplayers/evil-winrm'
license=('LGPL3')
# git required because it's used in gemspec and we bundle exec in the wrapper
depends=('ruby' 'ruby-bundler' 'libxslt' 'ruby-rexml' 'git' 'openssl')
makedepends=('git')
source=("git+https://github.com/Hackplayers/$pkgname.git")
sha512sums=('SKIP')
install="$pkgname.install"

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

package() {
  cd $pkgname

  install -dm 755 "$pkgdir/usr/bin"
  install -dm 755 "$pkgdir/usr/share/$pkgname"

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/" *.md

  cp -a --no-preserve=ownership resources/ "$pkgdir/usr/share/doc/$pkgname/"

  rm -r *.md resources/ .github/ LICENSE

  cp -a --no-preserve=ownership * "$pkgdir/usr/share/$pkgname/"
  # git used in gemspec so we need .git/ for bundle exec
  cp -a --no-preserve=ownership .git "$pkgdir/usr/share/$pkgname/"

  # workaround https://github.com/WinRb/WinRM/issues/340
  cp /etc/ssl/openssl.cnf "$pkgdir/usr/share/$pkgname/"
  sed -zi "s/\[provider_sect\]\ndefault = default_sect/\[provider_sect\]\ndefault = default_sect\nlegacy = legacy_sect/" "$pkgdir/usr/share/$pkgname/openssl.cnf"
  sed -zi "s/\[default_sect\]\n# activate = 1/\[default_sect\]\nactivate = 1\n\[legacy_sect\]\nactivate = 1/" "$pkgdir/usr/share/$pkgname/openssl.cnf"

  # Specify bundle's gemfile environment variable to avoid changing directories when running the program.
  cat > "$pkgdir/usr/bin/$pkgname" << EOF
#!/bin/sh

OPENSSL_CONF=/usr/share/$pkgname/openssl.cnf BUNDLE_GEMFILE=/usr/share/$pkgname/Gemfile exec bundle exec /usr/share/$pkgname/$pkgname.rb "\$@"
EOF

  chmod +x "$pkgdir/usr/bin/$pkgname"
}

